package gcdOfStrings;

public class Solution {
    public String gcdOfStrings(String str1, String str2) {
        if(str1.length()<str2.length()){
            String t=str1;
            str2=str1;
            str1=t;
        }//保证str1的长度是最长的
        for (int i = str2.length(); i > 0; i--) {
            if(str1.length()%i==0&&str2.length()%i==0){
                StringBuilder s=new StringBuilder();
                for (int j = 0; j < i; j++) {
                    s.append(str2.charAt(j));
                }
                StringBuilder t1=new StringBuilder();
                StringBuilder t2=new StringBuilder();
                int num2=str2.length()%i;
                int num1=str1.length()%i;
                for (int j = 0; j <num1 ; j++) {
                    t1.append(s);
                }
                for (int j = 0; j <num2 ; j++) {
                    t2.append(s);
                }
                if(t1.toString().equals(str1)&&t2.toString().equals(str2)){
                    return s.toString();
                }
            }
        }
        return "";
    }
}
